package com.deyuanyun.pic.pipe.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.caixu.beanvalidateutil.utils.ObjectUtil;
import com.deyuanyun.pic.common.cache.TempStoreDataSupport;
import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.common.util.MapUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.pipe.domain.exceldomain.PipeLineCentreImportDemo;
import com.deyuanyun.pic.pipe.domain.exceldomain.PipeSegmentImportDemo;
import com.deyuanyun.pic.pipe.mapper.DyyPipeLineMapper;
import com.deyuanyun.pic.pipe.mapper.DyyPipeSegmentMapper;
import com.deyuanyun.pic.pipe.service.PipeSegmentService;
import com.deyuanyun.pic.pipe.support.utils.UniformCodeUtils;
import com.deyuanyun.pic.support.biz.CodeUtil;
import com.github.pagehelper.Page;

@Service("segmentService")
public class PipeSegmentServiceImpl implements PipeSegmentService {

	@Resource
	private DyyPipeSegmentMapper dyyPipeSegmentMapper;

	@Resource
	private DyyPipeLineMapper dyyPipeLineMapper;
	@Override
	public int delGis(Map data) {
		dyyPipeSegmentMapper.delSegmentGis(data);
		return dyyPipeSegmentMapper.delGis(data);
	}

	@Override
	public Page<HashMap<String, Object>> findPage(HashMap map) {
		Page<HashMap<String, Object>> data=dyyPipeSegmentMapper.findPage(map);
		for(HashMap<String, Object> m:data)
		{
			m.put("PIPELINE_TYPE", CodeUtil.getName((String)m.get("PIPELINE_TYPE")));
			m.put("OPERATIONAL_STATUS", CodeUtil.getName((String)m.get("OPERATIONAL_STATUS")));
			m.put("DIAMETER", CodeUtil.getName((String)m.get("DIAMETER")));
			m.put("MATERIAL", CodeUtil.getName((String)m.get("MATERIAL")));
			m.put("MANUPIPE_TYPE", CodeUtil.getName((String)m.get("MANUPIPE_TYPE")));
			m.put("COATINGMATERIAL", CodeUtil.getName((String)m.get("COATINGMATERIAL")));
			m.put("COATING_GRADE", CodeUtil.getName((String)m.get("COATING_GRADE")));
			m.put("JOINT_MATERIAL", CodeUtil.getName((String)m.get("JOINT_MATERIAL")));
			m.put("INCOATING_MATERIAL", CodeUtil.getName((String)m.get("INCOATING_MATERIAL")));
			m.put("CP_TYPE", CodeUtil.getName((String)m.get("CP_TYPE")));
			m.put("MEDIUM_TYPE", CodeUtil.getName((String)m.get("MEDIUM_TYPE")));
			m.put("DRY_OR_WET", CodeUtil.getName((String)m.get("DRY_OR_WET")));
			m.put("PIPE_FUNCTION", CodeUtil.getName((String)m.get("PIPE_FUNCTION")));
		}
		MapUtil.emptyConvert(data);
		
		return data;
	}

	@Override
	public boolean importPoint(List<PipeLineCentreImportDemo> data) throws Exception {
		Map<String, Object> bean = null;
		
		for (PipeLineCentreImportDemo info : data) {
			bean = MapUtil.beanToMap(info);
			bean.put("id", UUID.randomUUID().toString().replaceAll("-", ""));
			int length=info.getGatherNo().length();
			bean.put("sort", info.getGatherNo().substring(length-4, length));
			
			dyyPipeSegmentMapper.insertPosit(bean);
			dyyPipeSegmentMapper.insertSegmentPosit(bean);
			
		}
		return true;
	}

	@Override
	public String importData(List<PipeSegmentImportDemo> data) throws Exception {
		StringBuffer result=new StringBuffer();
		Map<String, Object> bean = null;
		Properties p= TempStoreDataSupport.pullDataf("application.properties");
		String companyCode=p.getProperty("companyCode");
		List<Map<String, Object>> waitInsertList=new ArrayList<Map<String, Object>>();
		Map<String,String> segmentCheck=new HashMap<String,String>();
		for (PipeSegmentImportDemo info : data) {
			
			bean = MapUtil.beanToMap(info);
			MapUtil.emptyConvertString(bean);
			bean.put("id", UUID.randomUUID().toString().replaceAll("-", ""));
			String abridgeLine= dyyPipeLineMapper.findlineAbridge(info.getPipeLineId());
			String abridgeSegment= dyyPipeSegmentMapper.findlineAbridge(info.getSegment());
			
			if(StringUtils.isEmpty(info.getCode())){
				if(StringUtils.isEmpty(info.getInservicetime()))
				{
					bean.put("code",companyCode+abridgeLine+abridgeSegment+StringUtils.convertDateToString(new Date(), "yyyyMMdd"));
				}else{
					bean.put("code",companyCode+abridgeLine+abridgeSegment+bean.get("inservicetime"));
				}
			}
			
			
			Map<String, Object> segmentInfo=dyyPipeSegmentMapper.findInfo(bean);
			
			/**
			 * 判断管段信息是否已经插入
			 */
			if(null != segmentInfo.get("INFO_ID"))
			{
				result.append(segmentInfo.get("LINENAME")).append("=>").append(segmentInfo.get("SEGMENTNAME")).append("基本信息已经存在，请删除后再导入。");
			}else if(null != segmentCheck.get((String)bean.get("segment")))
			{
				result.append(segmentInfo.get("LINENAME")).append("=>").append(segmentInfo.get("SEGMENTNAME")).append("相同管段多次导入。");
				return result.toString();
			}
			else{
				segmentCheck.put((String)bean.get("segment"), "存在");
				waitInsertList.add(bean);
			}
		}
		/**
		 * 如果无错
		 */
		if(result.length() == 0)
		{
			/**
			 * 循环插入
			 */
			for (Map<String, Object> info : waitInsertList) 
			{
				dyyPipeSegmentMapper.insertData(info);
			}
		}
		return result.length()==0?null:result.toString();
	}

	@Override
	public List<List<String[]>> excelExport(Map map) {
		List<LinkedHashMap<String, Object>> data = dyyPipeSegmentMapper.exportExcel(map);
		for(LinkedHashMap<String, Object> m:data)
		{
			m.put("PIPELINE_TYPE", CodeUtil.getName((String)m.get("PIPELINE_TYPE")));
			m.put("OPERATIONAL_STATUS", CodeUtil.getName((String)m.get("OPERATIONAL_STATUS")));
			m.put("DIAMETER", CodeUtil.getName((String)m.get("DIAMETER")));
			m.put("MATERIAL", CodeUtil.getName((String)m.get("MATERIAL")));
			m.put("MANUPIPE_TYPE", CodeUtil.getName((String)m.get("MANUPIPE_TYPE")));
			m.put("COATINGMATERIAL", CodeUtil.getName((String)m.get("COATINGMATERIAL")));
			m.put("COATING_GRADE", CodeUtil.getName((String)m.get("COATING_GRADE")));
			m.put("JOINT_MATERIAL", CodeUtil.getName((String)m.get("JOINT_MATERIAL")));
			m.put("INCOATING_MATERIAL", CodeUtil.getName((String)m.get("INCOATING_MATERIAL")));
			m.put("CP_TYPE", CodeUtil.getName((String)m.get("CP_TYPE")));
			m.put("MEDIUM_TYPE", CodeUtil.getName((String)m.get("MEDIUM_TYPE")));
			m.put("DRY_OR_WET", CodeUtil.getName((String)m.get("DRY_OR_WET")));
			m.put("PIPE_FUNCTION", CodeUtil.getName((String)m.get("PIPE_FUNCTION")));
			
			
		}
		MapUtil.emptyConvertLink(data);
		List<List<String[]>> result=new ArrayList<List<String[]>>();
		List<String[]> lsr = JXL.toList(data, "yyyy-MM-dd");
		result.add(lsr);
		return result;
	}

	@Override
	public int delInfo(Map map) {
		return dyyPipeSegmentMapper.delInfo(map);
	}

	@Override
	public int checkGisNum(String id) {
		// TODO Auto-generated method stub
		return dyyPipeSegmentMapper.checkGisNum(id);
	}

	@Override
	public List<HashMap<String, Object>> findGis(String lineId) {
		return dyyPipeSegmentMapper.findGis(lineId);
	}

	@Override
	public Map<String, Object> findSegmentById(String lineId) {
		return dyyPipeSegmentMapper.findSegmentById(lineId);
	}

	

	

}
